﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewsAggregatorLibrary.DAL;

namespace NewsAggregatorLibrary
{
    public class RelationMember : KeywordStoryRelation
    {
        /// <summary>
        /// Retrieves all Relations from the DB.
        /// </summary>
        /// <returns></returns>
        public static RelationMemberCollection RetrieveAll()
        {
            try
            {
                return DB.Select().From(Schema).ExecuteAsCustomCollection<KeywordStoryRelation, KeywordStoryRelationCollection, RelationMember, RelationMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while generating a list of Relations " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a RelationMemberCollection by KeyID.
        /// </summary>
        /// <returns></returns>
        public static RelationMemberCollection RetrieveByKeyID(int keyID)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.KeyID).IsEqualTo(keyID).ExecuteAsCustomCollection<KeywordStoryRelation, KeywordStoryRelationCollection, RelationMember, RelationMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving relations by keyID " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a RelationMemberCollection by KeyID.
        /// </summary>
        /// <returns></returns>
        public static RelationMemberCollection RetrieveByKeyword(string keyword)
        {
            try
            {
                KeywordsMemberCollection keywordIDs = KeywordsMember.RetrieveLikeKeyword(keyword);
                List<int> list = keywordIDs.AsEnumerable().Select(o => o.Id).ToList();
                return DB.Select().From(Schema).Where(Columns.KeyID).In(list).ExecuteAsCustomCollection<KeywordStoryRelation, KeywordStoryRelationCollection, RelationMember, RelationMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving relations by keyID " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a RelationMemberCollection by StoryID.
        /// </summary>
        /// <returns></returns>
        public static RelationMemberCollection RetrieveByStoryID(int storyID)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.StroyID).IsEqualTo(storyID).ExecuteAsCustomCollection<KeywordStoryRelation, KeywordStoryRelationCollection, RelationMember, RelationMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving relations by storyID " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a RelationMember by StoryID and keyID.
        /// </summary>
        /// <returns></returns>
        public static RelationMember RetrieveByStoryIDKeyID(int storyID,int keyID)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.StroyID).IsEqualTo(storyID).And(Columns.KeyID).IsEqualTo(keyID).ExecuteSingle<RelationMember>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving relations by storyID and keyID " + e.Message, e);
            }
        }

        /// <summary>
        /// Adds a new Relation to the DB.
        /// </summary>
        /// <param name="keyID">The key ID.</param>
        /// <param name="storyID">The story ID.</param>
        /// <exception cref="System.Exception"></exception>
        public static void AddRelation(int keyID, int storyID)
        {
            RelationMember relation = new RelationMember();
            try
            {
                relation.KeyID = keyID;
                relation.StroyID = storyID;
                relation.Save();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while inserting a new relation. " + e.Message);
            }
        }
    }

    public class RelationMemberCollection : KeywordStoryRelationCollection
    {
    }
}
